package entity

import (
	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//CDZYCXRJDR 存单质押储蓄日均导入
type CDZYCXRJDR struct {
	ID   int     `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	KHYF int     `gorm:"column:KHYF"`
	ZJHM string  `gorm:"column:ZJHM;size:100"`
	RJ   float64 `gorm:"column:RJ"`
	DRJG string  `gorm:"column:DRJG;"`
}

//CdzycxrjBeforeImportValidation 导入前执行，验证数据
func CdzycxrjBeforeImportValidation(index int, rowdatas []string) []base.ErrorData {
	sql := "SELECT ID FROM T_INDI_CLIE WHERE IDNO=?"
	rows, _ := base.DB.Raw(sql, rowdatas[0]).Rows()
	var CUID int
	if rows.Next() {
		rows.Scan(&CUID)
	}
	errDatas := make([]base.ErrorData, 1, 1)
	if CUID == 0 {
		errInfos := make([]base.ErrorInfo, 1, 1)
		errinfo := base.ErrorInfo{
			ColIndex: 0,
			Message:  "证件号码错误错误或不存在！",
		}
		errInfos[0] = errinfo
		errData := base.ErrorData{
			RowIndex:   index,
			DataValues: rowdatas,
			ErrorInfos: errInfos,
		}
		errDatas[0] = errData
		return errDatas
	}
	return nil
}

//CdzycxrjAfterImport 导入后执行
func CdzycxrjAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	// err = tx.Exec("UPDATE T_BASE_YXFY_CDZYCXRJ SET CUID=B.ID "+
	// 	"FROM T_BASE_YXFY_CDZYCXRJ A INNER JOIN T_INDI_CLIE B ON  RTRIM(LTRIM(A.ZJHM)) = B.IDNO "+
	// 	"WHERE KHYF=?", params["KHYF"]).Error
	// if err != nil {
	// 	return err
	// }
	prams := make([]interface{}, 1)
	prams[0] = params["KHYF"]
	tx.Exec("EXEC UP_BASE_YXFY_CDZYCXRJ_DTIP ?", prams...)
	return nil
}

//TableName 存单质押储蓄日均导入表
func (CDZYCXRJDR) TableName() string {
	return "T_BASE_YXFY_CDZYCXRJ_DTIP"
}

//CDZYCXRJDREF 返回结构体
func CDZYCXRJDREF() base.EntityFunc {
	return func() interface{} {
		entity := CDZYCXRJDR{}
		return &entity
	}
}

//CDZYCXRJDRESF 返回结构体数组
func CDZYCXRJDRESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CDZYCXRJDR
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("CDZYCXRJDR", base.NewDefaultDao(CDZYCXRJDREF(), CDZYCXRJDRESF(), nil))
	//base.SetValidationCallBack("CDZYCXRJDR", CdzycxrjBeforeImportValidation)
	base.SetAfterCallBack("CDZYCXRJDR", CdzycxrjAfterImport)
}
